#include "ZText.h"
#include "ZPaintManager.h"
#include "ZControlFactory.h"



namespace ZuiLib
{

ZText::ZText(void)
	:m_dwTextColor(0xff000000), 
	m_dwDisabledTextColor(0xff808080)
{
	SetRectEmpty(&m_rcTextPadding);
	m_textDraw=ZTextDraw::CreateTextDraw();
	m_textDraw->SetAlign(DT_LEFT);
}


ZText::~ZText(void)
{
	m_textDraw=NULL;
}




const char* ZText::GetClass() const
{
	return "Text";
}


void* ZText::GetInterface(const char* pstrName)
{
	if( strcmp(pstrName,"Text") == 0 ) return static_cast<ZText*>(this);
	return ZControl::GetInterface(pstrName);
}

void ZText::SetTextStyle(uint32_t uStyle)
{
	m_textDraw->SetAlign(uStyle);
	Invalidate();
}


uint32_t ZText::GetTextStyle() const
{
	return m_textDraw->GetAlign();
}

void ZText::SetTextColor(color_t dwTextColor)
{
	m_dwTextColor = dwTextColor;
}


color_t ZText::GetTextColor() const
{
	return m_dwTextColor;
}
void ZText::SetDisabledTextColor(color_t dwTextColor)
{
	m_dwDisabledTextColor = dwTextColor;
}
color_t ZText::GetDisabledTextColor() const
{
	return m_dwDisabledTextColor;
}
void ZText::SetFontName(const char* font)
{
	m_textDraw->SetFont(font);
}

const char* ZText::GetFontName()
{
	return m_textDraw->GetFont();
}
RECT ZText::GetTextPadding() const
{
	return m_rcTextPadding;
}
void ZText::SetTextPadding(RECT rc)
{
	m_rcTextPadding=rc;
}

SIZE ZText::EstimateSize(SIZE szAvailable)
{
	return ZControl::EstimateSize(szAvailable);
}


void ZText::DoEvent(TEventUI& event)
{
	if( event.Type == UIEVENT_SETFOCUS ) 
	{
		m_bFocused = true;
		return;
	}
	if( event.Type == UIEVENT_KILLFOCUS ) 
	{
		m_bFocused = false;
		return;
	}
	if( event.Type == UIEVENT_MOUSEENTER )
	{
		return;
	}
	if( event.Type == UIEVENT_MOUSELEAVE )
	{
		return;
	}
	ZControl::DoEvent(event);
}


void ZText::SetAttribute(const char* pstrName, const char* pstrValue)
{
	if( strcmp(pstrName, "align") == 0 ) {
		uint32_t textStyle=m_textDraw->GetAlign();
		if( strstr(pstrValue, "left") != NULL ) {
			textStyle &= ~(DT_CENTER | DT_RIGHT | DT_TOP | DT_BOTTOM);
			textStyle |= DT_LEFT;
		}
		if( strstr(pstrValue, "center") != NULL ) {
			textStyle &= ~(DT_LEFT | DT_RIGHT | DT_TOP | DT_BOTTOM);
			textStyle |= DT_CENTER;
		}
		if( strstr(pstrValue, "right") != NULL ) {
			textStyle &= ~(DT_LEFT | DT_CENTER | DT_TOP | DT_BOTTOM);
			textStyle |= DT_RIGHT;
		}
		if( strstr(pstrValue, "top") != NULL ) {
			textStyle &= ~(DT_BOTTOM | DT_VCENTER | DT_LEFT | DT_RIGHT);
			textStyle |= DT_TOP;
		}
		if( strstr(pstrValue, "bottom") != NULL ) {
			textStyle &= ~(DT_TOP | DT_VCENTER | DT_LEFT | DT_RIGHT);
			textStyle |= DT_BOTTOM;
		}

		m_textDraw->SetAlign(textStyle);
	}
	else if( strcmp(pstrName, "endellipsis") == 0 ) 
	{
		uint32_t textStyle=m_textDraw->GetAlign();
		if( strcmp(pstrValue, "true") == 0 ) 
			textStyle |= DT_END_ELLIPSIS;
		else 
			textStyle &= ~DT_END_ELLIPSIS;
		m_textDraw->SetAlign(textStyle);
	}    
	else if( strcmp(pstrName, "font") == 0 ) 
		SetFontName(pstrValue);
	else if( strcmp(pstrName, "textcolor") == 0 ) 
	{
		if( *pstrValue == '#') pstrValue = CharNext(pstrValue);
		char* pstr = NULL;
		color_t clrColor = strtoul(pstrValue, &pstr, 16);
		SetTextColor(clrColor);
	}
	else if( strcmp(pstrName, "disabledtextcolor") == 0 ) 
	{
		if( *pstrValue == '#') pstrValue = CharNext(pstrValue);
		char* pstr = NULL;
		color_t clrColor = strtoul(pstrValue, &pstr, 16);
		SetDisabledTextColor(clrColor);
	}
	else if( strcmp(pstrName, "textpadding") == 0 ) 
	{
		RECT rcTextPadding = { 0 };
		char* pstr = NULL;
		rcTextPadding.left = strtol(pstrValue, &pstr, 10);  ZASSERT(pstr);    
		rcTextPadding.top = strtol(pstr + 1, &pstr, 10);    ZASSERT(pstr);    
		rcTextPadding.right = strtol(pstr + 1, &pstr, 10);  ZASSERT(pstr);    
		rcTextPadding.bottom = strtol(pstr + 1, &pstr, 10); ZASSERT(pstr);    
		SetTextPadding(rcTextPadding);
	}
	else
		ZControl::SetAttribute(pstrName, pstrValue);
}

void ZText::PaintText(ZRenderer* hDC)
{
	if( m_dwTextColor == 0 ) m_dwTextColor = m_pManager->GetDefaultFontColor();
	if( m_dwDisabledTextColor == 0 ) m_dwDisabledTextColor = m_pManager->GetDefaultDisabledColor();

	int nLinks = 0;
	RECT rc = m_rcItem;
	rc.left += m_rcTextPadding.left;
	rc.right -= m_rcTextPadding.right;
	rc.top += m_rcTextPadding.top;
	rc.bottom -= m_rcTextPadding.bottom;

	color_t color=m_dwTextColor;

	if( !IsEnabled() ) {
		color=m_dwDisabledTextColor;
	}

	m_textDraw->Draw(hDC,color,rc);
}

ZString ZText::GetText() const
{
	return m_textDraw->GetText();
}


void ZText::SetText(const char* pstrText)
{
	m_textDraw->SetText(pstrText);
}

void ZText::SetPos(RECT rc)
{
	RECT rcOld=GetPos();
	if(rcOld.right-rcOld.left !=rc.right-rc.left || rcOld.bottom-rcOld.top !=rc.bottom-rc.top)
		m_textDraw->OnControlResize();
	ZControl::SetPos(rc);
}

IMPL_ZCONTROL_FACTORY("Text",ZText);














}

